package cn.icanci.loopstack.bic.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.icanci.loopstack.bic.biz.dts.GraphDtsServiceProxy;
import cn.icanci.loopstack.bic.biz.mapper.NodeStyleMapper;
import cn.icanci.loopstack.bic.biz.service.NodeStyleService;
import cn.icanci.loopstack.bic.biz.utils.TemplateFileUtils;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.NodeStyleVO;
import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.NodeStyleDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.NodeStyleDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 22:45
 */
@Service
public class NodeStyleServiceImpl implements NodeStyleService {

    @Resource
    private NodeStyleDAO         nodeStyleDAO;
    @Resource
    private NodeStyleMapper      nodeStyleMapper;
    @Resource
    private GraphDtsServiceProxy graphDtsServiceProxy;

    @Override
    public List<NodeStyleVO> queryAll() {
        return nodeStyleMapper.dos2vos(nodeStyleDAO.queryAll());
    }

    @Override
    public void save(NodeStyleVO nodeStyleVO) {
        // TODO 日志记录
        if (doInsert(nodeStyleVO)) {
            NodeStyleDO insert = nodeStyleMapper.vo2do(nodeStyleVO);
            nodeStyleDAO.insert(insert);
        } else {
            nodeStyleDAO.update(nodeStyleMapper.vo2do(nodeStyleVO));
            graphDtsServiceProxy.dtsUnitViewTree(nodeStyleVO);
        }
    }

    @Override
    public NodeStyleVO queryById(String id) {
        return nodeStyleMapper.do2vo(nodeStyleDAO.queryOneById(id));
    }

    @Override
    public PageList<NodeStyleVO> queryPage(NodeStyleVO nodeStyleVO, int pageNum, int pageSize) {
        PageList<NodeStyleDO> pageQuery = nodeStyleDAO.pageQuery(nodeStyleMapper.vo2do(nodeStyleVO), pageNum, pageSize);
        return new PageList<>(nodeStyleMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());

    }

    @Override
    public NodeStyleVO queryBySystemUuidAndName(String systemUuid, String name) {
        return nodeStyleMapper.do2vo(nodeStyleDAO.queryBySystemUuidAndName(systemUuid, name));
    }

    @Override
    public NodeStyleVO queryBySystemUuidAndKey(String systemUuid, String key) {
        return nodeStyleMapper.do2vo(nodeStyleDAO.queryBySystemUuidAndKey(systemUuid, key));
    }

    @Override
    public List<TextValue> loadSelector(String systemUuid) {
        List<NodeStyleVO> nodeStyles = queryAll();
        List<TextValue> textValues = Lists.newArrayList();
        for (NodeStyleVO nodeStyle : nodeStyles) {

            if (!StringUtils.equals(systemUuid, nodeStyle.getSystemUuid())) {
                continue;
            }

            String label;
            if (isDeleted(nodeStyle)) {
                label = String.format(DELETED_FORMAT, nodeStyle.getNodeStyleName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, nodeStyle.getNodeStyleName());
            }
            String value = nodeStyle.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public String loadDefaultStyle() {
        return TemplateFileUtils.queryDefaultNodeStyleTemplate();
    }

    @Override
    public NodeStyleVO queryByUuid(String styleUuid) {
        return nodeStyleMapper.do2vo(nodeStyleDAO.queryByUuid(styleUuid));
    }
}
